<?php

namespace apiend\modules\work\models;

use Yii;
use yii\helpers\ArrayHelper;
use common\widgets\phpcsv\CsvExport;

/**
 * SurveyDownloadSearch is the model of survey download class.
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SurveyDownloadSearch extends SurveySearch {

    public function getCheckedStatusText() {
        return $this->checkedStatus ? '是' : '否';
    }

    /**
     * {@inheritdoc}
     */
    public function exportFields() {
        return [
            'id',
            'realname:excelText',
            'mobile:excelText',
            'idcardno:excelText',
            'address:excelText',
            'checkedStatusText',
            'checkedTime:datetime',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => '居民ID',
            'user_id' => '用户ID',
            'realname' => '姓名',
            'mobile' => '手机号',
            'mobile_hash' => '加密手机号',
            'idcardno' => '身份证号',
            'idcardno_hash' => '加密身份证号',
            'village_id' => '社区ID',
            'family_id' => '家庭ID',
            'address' => '联系地址',
            'round_id' => '轮次ID',
            'status' => '状态',
            'created_at' => '注册时间',
            'updated_at' => '更新时间',
            'deleted_at' => '注销时间',
            //
            'checkedStatusText' => '是否已检测',
            'checkedTime' => '检测时间',
        ];
    }

    /**
     * 生成下载文件的名称
     * @return string
     */
    public function exportFileName($ext = true) {
        return Yii::t('message', '{time}-{title}{ext}', [
            'time' => date('Y年m月d日'),
            'title' => '检测状态信息表',
            'ext' => $ext ? '.csv' : '',
        ]);
    }

    /**
     * @return string
     */
    public function exportCsv() {
        if (!$this->validate()) {
            return $this->dumpError('');
        }
        ini_set('memory_limit', '500M');
        $query = $this->buildQuery();
        $query->debug();
        return CsvExport::export([
            'query' => $query,
            'fileName' => $this->exportFileName(),
            'asAttachment' => true,
            'columns' => $this->exportFields(),
            'headers' => $this->attributeLabels(),
        ]);
    }

}
